﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using CustomsSystem.BLL;
using System.Data;
using Maticsoft.Common;
using Microsoft.Win32;

namespace WEB.contract
{
    public partial class View : System.Web.UI.Page
    {
        ContractManage bll = new ContractManage();
        DataSet ds = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["ID"].ToString();
                if (id != null)
                {
                    //Cu_Contract model=new Cu_Contract();
                    //获取公司的全部合同数据，取最近时间
                    ds = bll.GetList(" CID= " + id + " order by id desc");
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        DataRow dr = ds.Tables[0].Rows[0];
                        //获取公司名
                        CustomerManage cusbll = new CustomerManage();
                        Customer cusmodel = cusbll.GetModel(Convert.ToInt32(dr["CID"].ToString()));
                        //
                        lbCID.Text = cusmodel.CustomerName.ToString();
                        ViewState["CID"] = id;
                        lbNO.Text = dr["ContractNO"].ToString();
                        string date1 = Convert.ToDateTime(dr["Datestart"].ToString()).ToShortDateString();
                        string date2 = "";
                        if (dr["Dateover"].ToString() != "")
                        {
                            date2 = Convert.ToDateTime(dr["Dateover"].ToString()).ToShortDateString();
                        }
                        else
                        {
                            date2 = "持续有效";
                        }
                        lbDate.Text = date1 + "~" + date2;
                        lbRE.Text = dr["Remarks"].ToString();
                        ViewState["path"] = dr["Images"].ToString();
                        ViewState["ID"] = dr["ID"].ToString();
                        lbCount.Text = ds.Tables[0].Rows.Count.ToString();//合同数
                    }
                    else
                    {
                        MessageBox.ShowAndRedirect(this, "还没有上传合同！请上传！", "/contract/Add.aspx?AddID=" + id);
                    }
                }
            }

        }

        protected void btdow_Click(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists(ViewState["path"].ToString()))
            {
                MessageBox.Show(this, "文件丢失！");
            }
            else
            {
                string fileName = ViewState["path"].ToString();
                if (System.IO.Path.GetExtension(fileName) == ".html")
                {
                    fileName = fileName.Replace(".html", ".doc");
                }
                DownloadFile(fileName);
            }
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            if (ViewState["ID"] != null)
            {
                if (bll.Delete(Convert.ToInt32(ViewState["ID"].ToString())))
                {
                    if (ViewState["path"] != null && System.IO.File.Exists(ViewState["path"].ToString()))
                    {
                        string fileName = ViewState["path"].ToString();
                        System.IO.File.Delete(fileName);
                    }
                    MessageBox.ShowAndRedirect(this, "删除成功", "/customer/customerView.aspx");
                }
                else
                {
                    MessageBox.Show(this, "删除失败");
                }
            }
            else
            {
                MessageBox.Show(this, "删除失败");
            }
        }
        //// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        protected void DownloadFile(string filename)
        {
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            string encodefileName = System.Web.HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8);
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();
        }

        protected void return_Click(object sender, EventArgs e)
        {
            Response.Redirect("./Add.aspx");
        }

    }
}